package net.web.inst.struts2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.esj.basic.exception.MelonException;
import net.esj.basic.service.DefaultService;
import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.Validators;
import net.melon.article.ArticleConstants;
import net.melon.article.pojo.Article;
import net.melon.order.inst.service.OrderService;
import net.melon.order.pojo.CarLease;
import net.melon.page.inst.service.ModuleService;
import net.melon.product.inst.service.ProductService;
import net.melon.product.inst.service.impl.ClassifyCacheProxy;
import net.melon.product.pojo.Product;
import net.melon.product.pojo.ProductClassify;
import net.melon.review.inst.service.ReviewService;
import net.melon.review.pojo.Area;
import net.melon.uploader.Uploader;
import net.web.dto.BaseParam;
import net.web.dto.City;
import net.web.dto.ParamArticle;
import net.web.dto.ParamDto;
import net.web.dto.ProductClassifyDto;
import net.web.dto.ProductDetail;
import net.web.dto.ProductDto;
import net.web.dto.UnicodeParamDto;
import net.web.inst.service.WebService;

public class DataAction extends WebAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4789304533638829949L;
	
	/**
	 * 当前页
	 */
	private Integer page;
	
	/**
	 * 每页行数
	 */
	private Integer rp;
	
	@Resource
	private ModuleService moduleService;
	
	@Resource
	private ClassifyCacheProxy classifyCacheProxy;
	
	@Resource
	private DefaultService defaultService;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private WebService webService;
	/**品牌*/
	private Long brands;
	/**厂商*/
	private Long manufacturers;
	/**车系*/
	private Long cars;
	
	private Double price1;
	
	private Double price2;
	
	private Integer orderBy1;//1升序，2降序
	
	private Integer orderBy2;//1升序，2降序
	
	private Long regionId;
	
	private String searchStr;
	
	private CarLease carLease;
	
	@Resource
	private OrderService orderService;
	
	@Resource
    private ReviewService reviewService;	
	
	public void productPage(){
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			if(page==null || page==0){
				page=1;
			}
			if(rp==null || rp==0){
				rp=12;
			}
			Pagination<Product> pagination = webService.productPage(page,rp,manufacturers,brands,cars,price1,price2,orderBy1,orderBy2,searchStr,getCitySession());
			for(Product product : pagination.getRows()){
				if(!Validators.isEmpty(product.getImages())){
					product.setIcon(Uploader.getDownUrl(Product.ICON_SUB_DIR) + product.getImages().get(0).getIcon());
				}else{
					product.setIcon(Uploader.getDownUrl(Product.ICON_SUB_DIR));
				}
			}
			List<BaseParam> seriesList = webService.getSeriesList();
			map.put("seriesList", seriesList);
			if(manufacturers!=null){
				List<BaseParam> brandList = webService.getBrandList(manufacturers);
				map.put("brandList", brandList);
			}
			if(brands!=null){
				List<BaseParam> classifyList = webService.getClassifyList(brands);
				map.put("classifyList", classifyList);
			}
			map.put("pagination", pagination);
			writeJson(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置城市
	 * */
	public void setCity(){
		setCitySession(regionId);
	}
	
	/**
	 * 获取城市
	 * */
	public void getCityList(){
		List<City> list = webService.getCityList();
		writeJson(list);
	}
	
	public void articlePage(){
		if(page==null || page==0){
			page=1;
		}
		if(rp==null || rp==0){
			rp=6;
		}
		Pagination<Article> pagination = webService.articlePage(page,rp,1);
		for(Article article : pagination.getRows()){
			if(StringUtils.hasText(article.getIcon())){
				article.setIcon(Uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY) + article.getIcon());
			}
		}
		writeJson(pagination);
	}
	
	public void click(){
		if(StringUtils.hasText(id)){
			try {
				productService.click(id);
				map.put("suc", suc);
			} catch (Exception e) {
				map.put("suc", fail);
			}
		}else{
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	
	public void getProductDetail(){
		try {
			ProductDetail dto = webService.getProductDetail(id);
			map.put("suc", suc);
			map.put("product", dto);
		} catch (MelonException e) {
			map.put("suc", fail);
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void getArticleDetail(){
		if(StringUtils.hasText(id)){
			try {
				Article article = webService.getArticleDetail(Integer.valueOf(id));
				if(StringUtils.hasText(article.getIcon())){
					article.setIcon(Uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY) + article.getIcon());
				}
				map.put("suc", suc);
				map.put("article", article);
			} catch (NumberFormatException e) {
				map.put("suc", fail);
			}catch (MelonException e) {
				map.put("suc", fail);
			} catch (Exception e) {
				map.put("suc", fail);
			}
		}else{
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	/**首页*/
	/**广告*/
	public void getAdvList(){
		List<ParamDto> dtos = webService.getAdvList();
		writeJson(dtos);
	}
	/**品牌*/
	public void getBrandList(){
		List<UnicodeParamDto> dtos = webService.getBrandList();
		writeJson(dtos);
	}
	/**活动*/
	public void getHotActivityList(){
		List<ParamDto> dtos = webService.getHotActivityList(true);
		writeJson(dtos);
	}
	/**活动列表*/
	public void getActivityList(){
		List<ParamDto> dtos = webService.getHotActivityList(false);
		writeJson(dtos);
	}
	/**热门车型*/
	public void getHotCarList(){
		List<ProductDto> dtos = webService.getHotCarList(getCitySession());
		for(ProductDto dto : dtos){
			if(StringUtils.hasText(dto.getIcon())){
				dto.setIcon(Uploader.getDownUrl(Product.ICON_SUB_DIR) + dto.getIcon());
			}
		}
		writeJson(dtos);
	}
	/**新闻资讯*/
	public void getNewsInformationList(){
		List<ParamArticle> dtos = webService.getNewsInformationList();
		for(ParamArticle dto : dtos){
			dto.getArticle().setIcon(Uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY)+dto.getArticle().getIcon());
		}
		writeJson(dtos);
	}
	/**印象够买*/
	public void getBuyPurchaseList(){
		try {
			List<ParamArticle> dtos = webService.getBuyPurchaseList();
			for(ParamArticle dto : dtos){
				dto.getArticle().setIcon(Uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY)+dto.getArticle().getIcon());
			}
			writeJson(dtos);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**以租代购*/
	public void getCarLeaseList(){
		try {
			List<ParamArticle> dtos = webService.getCarLeaseList();
			for(ParamArticle dto : dtos){
				dto.getArticle().setIcon(Uploader.getDownUrl(ArticleConstants.ARTICLE_CONTENT_ICON_UPLOAD_KEY)+dto.getArticle().getIcon());
			}
			writeJson(dtos);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void saveCarLease(){
		try {
			orderService.saveCarLease(carLease);
			map.put("suc",suc);
		} catch (Exception e) {
			map.put("suc",fail);
		}
		writeJson(map);
	}
	
	public void getClassifyList(){
		List<ProductClassify> list = classifyCacheProxy.getProductClassifyList();
		List<ProductClassifyDto> dtos = new ArrayList<ProductClassifyDto>();
		for(ProductClassify pc : list){
			ProductClassifyDto dto = new ProductClassifyDto(pc.getId(), pc.getName(), pc.getOrder(), pc.getLevel(), pc.getParent()==null?null:pc.getParent().getId());
			dtos.add(dto);
		}
		writeJson(dtos);
	}
	
	public void getProvinceList(){
		List<Area> list = reviewService.getAreaByParentId(0);
		Collections.sort(list, new Comparator<Area>() {
			@Override
			public int compare(Area o1, Area o2) {
				if(o1.getId() > o2.getId()){
					return 1;
				}else if(o1.getId() < o2.getId()){
					return -1;
				}
				return 0;
			}
		});
		writeJson(list);
	}
	
	public void getProvinceChildrenList(){
		if(StringUtils.hasText(id)){
			List<Area> list = reviewService.getAreaByParentId(Integer.valueOf(id));
			writeJson(list);
		}else{
			List<Area> list = reviewService.getAreaByParentId(100);
			writeJson(list);
		}
	}
	
	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRp() {
		return rp;
	}

	public void setRp(Integer rp) {
		this.rp = rp;
	}

	public Long getBrands() {
		return brands;
	}

	public void setBrands(Long brands) {
		this.brands = brands;
	}

	public Long getManufacturers() {
		return manufacturers;
	}

	public void setManufacturers(Long manufacturers) {
		this.manufacturers = manufacturers;
	}

	public Long getCars() {
		return cars;
	}

	public void setCars(Long cars) {
		this.cars = cars;
	}

	public Double getPrice1() {
		return price1;
	}

	public void setPrice1(Double price1) {
		this.price1 = price1;
	}

	public Double getPrice2() {
		return price2;
	}

	public void setPrice2(Double price2) {
		this.price2 = price2;
	}

	public Integer getOrderBy1() {
		return orderBy1;
	}

	public void setOrderBy1(Integer orderBy1) {
		this.orderBy1 = orderBy1;
	}

	public Integer getOrderBy2() {
		return orderBy2;
	}

	public void setOrderBy2(Integer orderBy2) {
		this.orderBy2 = orderBy2;
	}

	public Long getRegionId() {
		return regionId;
	}

	public void setRegionId(Long regionId) {
		this.regionId = regionId;
	}

	public String getSearchStr() {
		return searchStr;
	}

	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}

	public CarLease getCarLease() {
		return carLease;
	}

	public void setCarLease(CarLease carLease) {
		this.carLease = carLease;
	}
	
}
